def header( type="text/html" ):
  """
  Generate a basic Content-Type HTTP response header. 

  Returns the header as a string.

  """
  out = "Content-Type: %s\n" % (type)
  return out

def start_html(args={'title':"untitled"}):
  """
  Generate the start portion of an HTML document.

  possible attributes for args dictionary:
  
   body    : optional new body start tag
   charset : default iso-8859-1
   doctype : content of !DOCTYPE tag
   lang    : default 'en-US'
   type    : default 'text/html'
   xmlns   : html xml namespace

   links      : optional list of <link> tags
   metas      : optional list of <meta> tags
   stylesheet : optional cascading style sheet loaded via <link>

   javascript : optional list of javascript external scripts
   style      : content for optional <style></style> tag_pair inside <head>    

  Returns the HTML fragment as a string.
  """
  settings = {
   "body"    : '<body>',
   "xmlns"   : 'http://www.w3.org/1999/xhtml',
   "lang"    : 'en-US',
   "charset" : 'iso-8859-1',
   "type"    : 'text/html',
   "doctype" : 'html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ' +
               '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"'  }

  for x in settings.keys():
    if args.has_key(x):
      settings[x] = args[x]

  html_attribs = 'xmlns="%s" lang="%s" xml:lang="%s"' % ( settings["xmlns"], settings["lang"], settings["lang"] )

  out = "<!DOCTYPE %s>\n" % (settings["doctype"])
  out += "<html %s>\n<head><title>%s</title>\n" % (html_attribs, args["title"])
  for single in ("links","metas"):
    if args.has_key(single):
      for ele in args[single]:
        out += ele
  
  if args.has_key("stylesheet"):
    out += link({"rel":"stylesheet","type":"text/css","href":args["stylesheet"]})
  
  if settings["type"] and settings["charset"]:
    out += meta({"http-equiv":"Content-Type", 
             "content":"%s; charset=%s" % (settings["type"],settings["charset"])}) 

  if args.has_key("javascript"):
    for js in args["javascript"]:
      out += script({"type":"text/javascript","language":"javascript","src":js},"")

  if args.has_key("style"):
    out += style({"type":"text/css"},args["style"])

  out += "</head>\n%s" % settings["body"]
  return out

def end_html():
  return "</body></html>"

def tag_single(tag=None,args={}):
  if (tag != None):
    out = tag_attributes(tag,args)
    out += ' />'
    return out

def tag_attributes(tag=None,args={}):
  if (tag != None):
    out = '<%s' % tag
    for key, val in args.iteritems():
      out += ' %s="%s"' % (key,val)
    return out

def tag_pair(tag=None,content=None,args={}):
  if (tag != None) and (content != None):
    out = tag_attributes(tag,args)
    out += '>%s' % content
    out += '</%s>' % tag
    return out

def tag_begin(tag=None,args={}):
  if (tag != None):
    out = tag_attributes(tag,args)
    out += '>'
    return out

def tag_end(tag=None):
  if (tag != None):
    out = '</%s>' % tag
    return out

def h1(content=None,args={}):
  return tag_pair("h1",content,args)
def h2(content=None,args={}):
  return tag_pair("h2",content,args)
def h3(content=None,args={}):
  return tag_pair("h2",content,args)
def h4(content=None,args={}):
  return tag_pair("h2",content,args)
def h5(content=None,args={}):
  return tag_pair("h2",content,args)
def h6(content=None,args={}):
  return tag_pair("h2",content,args)
def h7(content=None,args={}):
  return tag_pair("h2",content,args)
def h8(content=None,args={}):
  return tag_pair("h2",content,args)

def tr(content=None,args={}):
  return tag_pair("tr",content,args)
def th(content=None,args={}):
  return tag_pair("th",content,args)
def td(content=None,args={}):
  return tag_pair("td",content,args)
def table(content=None,args={}):
  return tag_pair("table",content,args)
def start_table(args={}):
  return tag_begin("table",args)
def end_table():
  return tag_end("table") + "\n"

def a(content=None,args={}):
  return tag_pair("a",content,args) 
def i(content=None,args={}):
  return tag_pair("i",content,args)
def b(content=None,args={}):
  return tag_pair("b",content,args)
def p(content=None,args={}):
  return tag_pair("p",content,args)
def pre(content=None,args={}):
  return tag_pair("pre",content,args)

def ul(content=None,args={}):
  return tag_pair("ul",content,args)
def li(content=None,args={}):
  return tag_pair("li",content,args)
def script(args={"type":"text/javascript"},content=None):
  return tag_pair("script",content,args)
def style(args={"type":"text/css"},content=None):
  return tag_pair("style",content,args)
def blockquote(content=None,args={}):
  return tag_pair("blockquote",content,args)
def div(content=None,args={}):
  return tag_pair("div",content,args)
def center(content=None,args={}):
  return tag_pair("center",content,args)


def form(content=None,args={}):
  return tag_pair("form",content,args)

def start_form(args={}):
  return "\n"  + tag_begin("form",args) + "\n"
def end_form():
  return tag_end("form") + "\n"

def form_hidden(args={}):
  args["type"] = "hidden"
  return tag_single("input",args) + "\n"

def form_password(args={}):
  args["type"] = "password"
  return tag_single("input",args) + "\n"

def form_radio(args={}):
  args["type"] = "radio"
  return tag_single("input",args) + "\n"

def form_checkbox(args={}):
  args["type"] = "checkbox"
  return tag_single("input",args) + "\n"

def form_reset(args={}):
  args["type"] = "reset"
  return tag_single("input",args) 

def form_submit(args={}):
  args["type"] = "submit"
  return tag_single("input",args)

def form_textarea(content=None,args={}):
  return tag_pair("textarea",content,args) + "\n"

def form_textbox(args={}):
  args["type"] = "text"
  return tag_single("input",args) + "\n"

def form_button(args={}):
  args["type"] = "button"
  return tag_single("input",args)

def form_option(args={},value=None,content=None,selected=None):
  args["value"] = value
  contentfmt = "%s"
  if str(value) == str(selected):
    contentfmt = "*%s*"
    args["selected"] = "selected"
  return tag_pair("option",contentfmt % content,args) + "\n"

def form_select(args={},options={},selected=None):
  _content = ""
  for optvalue, opttext in options.iteritems():
    _content += form_option({},value=optvalue, content=opttext,selected=selected)
  return tag_pair("select",  _content, args) + "\n"

def img(args={}):
  return tag_single("img",args)
def br(args={}):
  return tag_single("br",args)
def meta(args={}):
  return tag_single("meta",args)
def link(args={}):
  return tag_single("link",args)
def hr(args={}):
  return tag_single("hr",args)

